/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

package net.zarubsys.unianalyzer.dataminers.lpt.internal.impl;

import net.zarubsys.unianalyzer.dataminers.lpt.ILptDataMiner;
import net.zarubsys.unianalyzer.dataminers.lpt.IMeasuringParametersWrapper;
import net.zarubsys.unianalyzer.dataminers.lpt.exceptions.AnalysisException;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.exceptions.UnianalyzerPortException;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.provider.ILptProvider;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.provider.LptProviderFactory;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.result.impl.Result;
import net.zarubsys.unianalyzer.dataminers.lpt.results.IResult;
import net.zarubsys.unianalyzer.dataminers.lpt.triggers.ITrigger;
import net.zarubsys.unianalyzer.dataminers.lpt.triggers.TriggerDescription;

import org.apache.log4j.Logger;

/**
 * LptDataMiner
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@media-solutions.cz&quot;&gt;Lukas Zaruba&lt;/A&gt;, MEDIA SOLUTIONS CZECH REPUBLIC Ltd.
 * @version $Revision$ $Date$
 */
public class LptDataMiner implements ILptDataMiner {

	private static final ILptDataMiner INSTANCE = new LptDataMiner();
	private static final Logger log = Logger.getLogger(LptDataMiner.class);
	
	private LptDataMiner() {
		
	}
	
	public static ILptDataMiner getInstance() {
		return INSTANCE;
	}

	private IResult analyze(int address, int samplesCount, int delay, ITrigger trigger, Result result) throws AnalysisException {
		ILptProvider portProvider = LptProviderFactory.getInstance().getLptProvider();
		try {
			portProvider.openLptPort(address);
			long time = System.nanoTime();
			int[] resultArray;
			if (trigger != null) {
				resultArray = portProvider.readMoreBytesWithTrigger(samplesCount, delay, trigger.getMask(), trigger.getFirstResult(), trigger.getSeconResult(), trigger.getMaxWaitInterval());
			} else {
				resultArray = portProvider.readMoreBytes(samplesCount, delay);
			}
			time = System.nanoTime() - time;
			result.setContent(resultArray);
			result.setNanoLength(time);
		} catch (UnianalyzerPortException e) {
			log.error("Error performing analysis.", e);
			throw new AnalysisException(e);
		} finally {
			try {
				portProvider.closeLptPort();
			} catch (UnianalyzerPortException e) {
				log.error("Error closing port \"" + address + "\"");
			}
		}
		return result;
	}

	public IResult analyze(IMeasuringParametersWrapper pw) throws AnalysisException {
		TriggerDescription td;
		if (pw.getTrigger() == null) {
			td = null;
		} else {
			td = new TriggerDescription(pw.getTriggerRisingEdge(), pw.getMaxWaitingInterval(), pw.getTriggerPin());
		}	
		Result result = new Result(pw.getFrequency(), pw.getFrequencyUnits(), pw.getAddress(), pw.getResultMask(), pw.getPinDescs(), td);
		int delay = getDelay();
		return analyze(pw.getAddress(), pw.getSamplesCount(), delay, pw.getTrigger(), result);
	}

	private int getDelay() {
		// TODO Create real delay method
		return 0;
	}

}


/*
 * $Log$
 */